#PURPOSE: Generate binary tree and linklist.


class TNode():
    def __init__(self,left=None,right=None,data=None):
        self.left=left
        self.right=right
        self.data=data

class LNode():
    def __init__(self,next=None,data=None):
        self.next=next
        self.data=data
        
def LINKEDLIST(*args):
    head=LNode()
    p=head
    for num in args:
        newNode=LNode(data=num)
        p.next=newNode
        p=newNode
    p.next=None
    
    return head
    

def BTREE(*args):
    def postOrder(idx):
        if(idx>=len(args) or args[idx]==None):
            return None
        left=postOrder(2*idx+1)
        right=postOrder(2*idx+2)
        father=TNode(left,right,args[idx])
        return father
        
    return postOrder(0)

def __testLinkedlist(head):
	p=head.next
	ans=0
	while(p!=None):
		ans+=p.data
		p=p.next
	print(ans)
def __testBtree(root):
	def preOrder(r):
		nonlocal ans
		if(r==None):
			return
		ans+=r.data
		preOrder(r.left)
		preOrder(r.right) 
	ans=0
	preOrder(root)
	print(ans)

if __name__=="__main__":
	__testLinklist(LINKEDLIST(3,1,6,4))	
	__testBtree(BTREE(3,2,4,1,None,None,2))
    
        
        